﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hxj.Data;
using Utility;
using System.Data;
using Utility.Json;
using Utility.Helper;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_Dept 的摘要说明
    /// </summary>
    public class Handler_Dept : BaseUI.BaseHandler
    {
        /// <summary>
        /// 获取下级个数
        /// </summary>
        public void GetChildrenCount()
        {
            string strFlag = "0";
            WhereClip wcp = new WhereClip();
            wcp = wcp.And("ParentID in (select DeptID from Sys_dept where ID='" + ParamsOfRequest.Request("DeptID") + "')");
            using (DataTable dt = BLL.BLLServiceHelper.GetDeptService().GetDeptTable(wcp))
            {
                strFlag = dt.Rows.Count.ToIntString();
            }
            base.Response.Write(strFlag);
        }
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            string strID = ParamsOfRequest.Request("DeptID");
            try
            {
                BLL.BLLServiceHelper.GetDeptService().Delete(strID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
        /// <summary>
        /// 创建组织部门树形
        /// </summary>
        public void GetDeptTree()
        {
            string strJson = string.Empty;

            WhereClip wcp = new WhereClip();
            using (DataTable dtTreeTable = BLL.BLLServiceHelper.GetDeptService().GetDeptTable(wcp))
            {
                TreeItem ti;
                string strEmp = Guid.Empty.ToString();
                ti = new TreeItem() { id = strEmp + "_0", text = "组织机构列表", value = strEmp, isexpand = true, showcheck = false };
                GetTreeDeptItemFromDataTable(ti, dtTreeTable, "0", false, null);
                strJson = ti.ToJsonString();
            }
            base.Response.Write(strJson);
        }

        #region 创建组织机构Tree
        DataTable DeptTable;
        string strTextName;
        List<string> ListValue;
        /// <summary>
        /// 由DataTable 生成树类
        /// </summary>
        /// <param name="ti">要装载的树类根节点</param>
        /// <param name="dt">数据表</param>
        /// <param name="sId">父类ID</param>
        /// <returns></returns>
        private void GetTreeDeptItemFromDataTable(TreeItem ti, DataTable dt, string sId, bool IsShowCheckBox, List<string> list)
        {
            ListValue = list;
            DeptTable = dt;
            strTextName = "DeptName";
            CreateOne(ti, sId, IsShowCheckBox);
            DeptTable.Dispose();
            strTextName = null;
            DeptTable = null;
            ListValue = null;
        }

        private void CreateOne(TreeItem ti, string sId, bool IsShowCheckBox)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = DeptTable.Select("ParentID='" + sId + "'", "Sort");
            foreach (DataRow dr in drs0)
            {
                parent = dr["DeptID"].ToString();
                ti0 = CreateItem(dr, parent, IsShowCheckBox, true);
                CreateNode(ti0, parent, IsShowCheckBox);
                ti.Add(ti0);
            }
        }

        private void CreateNode(TreeItem ti, string sId, bool IsShowCheckBox)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = DeptTable.Select("ParentID='" + sId + "'", "Sort");
            foreach (DataRow dr in drs0)
            {
                parent = dr["DeptID"].ToString();
                if (parent.Length <= 10)
                {
                    ti0 = CreateItem(dr, parent, IsShowCheckBox, true);
                }
                else
                {
                    ti0 = CreateItem(dr, parent, IsShowCheckBox, false);
                }
                CreateNode(ti0, parent, IsShowCheckBox);
                ti.Add(ti0);
            }
        }

        private TreeItem CreateItem(DataRow dr, string parent, bool IsShowCheckBox, bool IsExpand)
        {
            TreeItem ti0;
            if (ListValue != null && ListValue.Count > 0)
            {
                if (ListValue.Contains(dr["ID"].ToString()))
                {
                    ti0 = new TreeItem() { id = parent, value = dr["ID"].ToString(), text = dr[strTextName].ToString(), isexpand = IsExpand, showcheck = IsShowCheckBox, checkstate = 1 };
                }
                else
                {
                    ti0 = new TreeItem() { id = parent, value = dr["ID"].ToString(), text = dr[strTextName].ToString(), isexpand = IsExpand, showcheck = IsShowCheckBox };
                }
            }
            else
            {
                ti0 = new TreeItem() { id = parent, value = dr["ID"].ToString(), text = dr[strTextName].ToString(), isexpand = IsExpand, showcheck = IsShowCheckBox };
            }
            return ti0;
        }
        #endregion
    }
}